package com.wendao.code;

import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.zaxxer.hikari.HikariDataSource;

import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author sunfayun
 * @version 1.0
 * @since 2023/10/11
 */
public class CodeGen {

    private static final String[] TABLES = {"driver_info","driver_licence_info","driver_punish_info","driver_rate_info","driver_train_info","driver_wallet","driver_bill_info"};

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://rm-2vc94640284d287120o.mysql.cn-chengdu.rds.aliyuncs.com:3306/wendaochuxing");
        dataSource.setUsername("wendao");
        dataSource.setPassword("wendao@12345");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 注释
        globalConfig.getJavadocConfig().setAuthor("sunfayun");
        // 设置代码生成模块
        String basePath = System.getProperty("user.dir") + "/service-user/src/main";
        globalConfig.setSourceDir(basePath + "/java");
        globalConfig.setMapperXmlPath(basePath + "/resources/mapper");
        //设置根包
        globalConfig.setBasePackage("com.wendao.user.driver");

        //设置表前缀和只生成哪些表
//        globalConfig.setTablePrefix("tb_");
        globalConfig.setGenerateTable(TABLES);

        // 配置模板
        globalConfig.getTemplateConfig()
                .setController(System.getProperty("user.dir") + "/src/main/resources/templates/controller.tpl");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setOverwriteEnable(true)
                .setWithSwagger(true)
                .setSwaggerVersion(EntityConfig.SwaggerVersion.DOC);

        //设置生成 mapper
        globalConfig.enableMapper()
                .setOverwriteEnable(true);

        globalConfig.enableService()
                .setOverwriteEnable(true);
        globalConfig.enableServiceImpl()
                .setOverwriteEnable(true);
        globalConfig.enableController()
                .setOverwriteEnable(true);

        // mapper.xml文件
        globalConfig.enableMapperXml()
                .setOverwriteEnable(true);

        // 设置字段映射
        JdbcTypeMapping.registerMapping(Timestamp.class, LocalDateTime.class);
        JdbcTypeMapping.registerMapping(Date.class, LocalDate.class);
        JdbcTypeMapping.registerMapping(Time.class, LocalTime.class);
        JdbcTypeMapping.registerMapping(BigInteger.class, Long.class);

        //可以单独配置某个列
        globalConfig.setColumnConfig(ColumnConfig.create().setColumnName("id").setKeyType(KeyType.Auto));
        globalConfig.setColumnConfig(ColumnConfig.create().setColumnName("is_deleted").setLogicDelete(true));
        globalConfig.setColumnConfig(ColumnConfig.create().setColumnName("create_time").setOnInsertValue("now()"));
        globalConfig.setColumnConfig(ColumnConfig.create().setColumnName("update_time").setOnInsertValue("now()").setOnUpdateValue("now()"));

        return globalConfig;
    }

}
